﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class BorrowService : IBorrowService
    {
        private IBorrowDal _borrow;
        private IMapper _mapper;
        public BorrowService(IBorrowDal borrow, IMapper mapper)
        {
            _borrow = borrow;
            _mapper = mapper;
        }

        public async Task<int> AddBorrowAsync(int borrowDate)
        {
            return await _borrow.AddAsync(new Borrow 
            {
                BorrowDate = borrowDate
            });
        }

        public async Task<int> DeleteBorrowAsync(int id)
        {
            var data = await _borrow.QueryAsync(id);
            return data == null ? -1 : await _borrow.DeleteAsync(data);
        }

        public async Task<int> EditBorrowAsync(int id, int borrowDate)
        {
            var data = await _borrow.QueryAsync(id);
            if (data == null)
                return -1;
            data.BorrowDate = borrowDate;
            return await _borrow.EditAsync(data);
        }

        public async Task<List<BorrowDto>> QueryBorrowAllAsync()
        {
            var data = await _borrow.QueryAsync();
            return _mapper.Map<List<Borrow>, List<BorrowDto>>(data);
        }

        public async Task<BorrowDto> QueryBorrowByIdAsync(int id)
        {
            var data = await _borrow.QueryAsync(id);
            return _mapper.Map<Borrow, BorrowDto>(data);
        }
    }
}
